<template>
  <el-menu default-active="1-4-1"
           class="el-menu-vertical-demo"
           :default-openeds="opens"
           style="min-height:100%;overflow-x: hidden;"
           text-color="#606266"
           active-text-color="#409EFF"
           background-color="#ffffff"
           :collapse-transition="true"
           router
           :collapse="isCollapse">
    <div class="logo-container">
      <span class="logo-text" v-show="logoTextShow">链农</span>
    </div>
    
    <el-menu-item index="/blockchainStorage">
      <i class="el-icon-connection"></i>
      <span slot="title">区块链存储管理</span>
    </el-menu-item>
    
    <div v-for="item in menus" :key="item.id">
      <div v-if="item.path">
        <el-menu-item :index="item.path">
          <i :class="item.icon"></i>
          <span slot="title">{{item.name}}</span>
        </el-menu-item>
      </div>
      <div v-else>
        <el-submenu :index="item.id + ''">
          <template slot="title">
            <i :class="item.icon"></i>
            <span slot="title">{{item.name}}</span>
          </template>
          <div v-for="subItem in item.children" :key="subItem.id">
            <el-menu-item  :index="subItem.path">
              <template slot="title">
                <i :class="subItem.icon"></i>
                <span slot="title">{{subItem.name}}</span>
              </template>
            </el-menu-item >
          </div>
        </el-submenu>
      </div>

    </div>
  </el-menu>
</template>

<script>
export default {
  name: "Aside",
  props:{
    isCollapse:Boolean,
    logoTextShow:Boolean
  },
  data(){
    return{
      menus:localStorage.getItem("menus") ? JSON.parse(localStorage.getItem("menus")) : [],
      opens:localStorage.getItem("menus") ? JSON.parse(localStorage.getItem("menus")).map(v => v.id + '') : [],
    }
  },
  created() {
    if (this.menus && this.menus.length > 0) {
      const homeIndex = this.menus.findIndex(item => item.path === '/home' || item.name === '首页');
      
      if (homeIndex > 0) {
        const homeMenu = this.menus.splice(homeIndex, 1)[0];
        this.menus.unshift(homeMenu);
      }
    }

    this.fixIconIssues();
  },
  methods: {
    fixIconIssues() {
      if (!this.menus || this.menus.length === 0) return;
      
      this.menus.forEach(item => {
        if (item.name === '购物车' && (!item.icon || item.icon === 'ei-icon-shopping-cart-2')) {
          item.icon = 'el-icon-shopping-cart-2';
        }
        
        if (item.name === '购物' && (!item.icon || item.icon === 'ei-icon-present')) {
          item.icon = 'el-icon-present';
        }
        
        if (item.children && item.children.length > 0) {
          item.children.forEach(subItem => {
            if (subItem.name === '购物车' && (!subItem.icon || subItem.icon === 'ei-icon-shopping-cart-2')) {
              subItem.icon = 'el-icon-shopping-cart-2';
            }
            if (subItem.name === '购物' && (!subItem.icon || subItem.icon === 'ei-icon-present')) {
              subItem.icon = 'el-icon-present';
            }
          });
        }
      });
      
      localStorage.setItem('menus', JSON.stringify(this.menus));
    }
  }
}
</script>

<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
  border-right: none;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

/* 隐藏滚动条 */
::-webkit-scrollbar {
  width: 0 !important;
  height: 0;
}

/* Logo 样式 */
.logo-container {
  height: 60px;
  line-height: 60px;
  text-align: center;
  transition: all 0.3s;
  background-color: rgba(64, 158, 255, 0.1);
  margin-bottom: 15px;
  position: relative;
  overflow: hidden;
}

.logo-container::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 10%;
  width: 80%;
  height: 1px;
  background: linear-gradient(to right, transparent, rgba(64, 158, 255, 0.3), transparent);
}

.logo-text {
  color: #409EFF;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 2px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

/* 菜单项美化 */
.el-menu-item {
  transition: all 0.3s !important;
  border-radius: 0 20px 20px 0;
  margin: 4px 10px 4px 0;
  height: 50px;
  line-height: 50px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.15) !important;
  transform: translateX(5px);
}

.el-menu-item.is-active {
  background: linear-gradient(to right, rgba(64, 158, 255, 0), rgba(64, 158, 255, 0.15)) !important;
  border-right: 3px solid #409EFF;
  color: #409EFF !important;
  font-weight: bold;
}

.el-submenu__title {
  transition: all 0.3s !important;
  border-radius: 0 20px 20px 0;
  margin: 4px 10px 4px 0;
  height: 50px;
  line-height: 50px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.el-submenu__title:hover {
  background-color: rgba(255, 255, 255, 0.15) !important;
  transform: translateX(5px);
}

/* 子菜单项样式 */
.el-menu .el-menu {
  background-color: rgba(0, 0, 0, 0.2) !important;
  padding: 5px 0;
}

.el-menu .el-menu .el-menu-item {
  height: 40px;
  line-height: 40px;
  margin-left: 10px;
  font-weight: 500;
}

/* 图标样式 */
.el-menu i {
  margin-right: 5px;
  color: #909399;
  font-size: 18px;
  vertical-align: middle;
  transition: all 0.3s;
}

.el-menu-item.is-active i {
  color: #409EFF;
  transform: scale(1.1);
}

/* 边框样式 */
.el-menu {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  border-right: none !important;
}

/* 折叠菜单样式优化 */
.el-menu--collapse .logo-container {
  padding: 0;
}

.el-menu--collapse .el-menu-item {
  border-radius: 50%;
  margin: 10px auto;
  width: 35px;
  height: 35px;
  min-width: 35px;
  line-height: 35px;
  padding: 0 !important;
  text-align: center;
}

.el-menu--collapse .el-menu-item i {
  margin: 0;
  padding: 0;
}
</style>
